﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

using SdlDotNet.Audio;

namespace T7G_4_Win
{
    /// <summary>
    /// Music wrapper
    /// </summary>
    class GrvMusic
    {
        // Use SDL since simpler at this stage

        Music music;
        Music loop;

        CDDrive cd = null;

        /// <summary>
        /// Path to T7G file directory containing files
        /// </summary>
        private string path = Properties.Settings.Default.T7GPath;

        T7G game;

        /// <summary>
        /// Create new music player object
        /// </summary>
        public GrvMusic(T7G window)
        {
            game = window;
            path = @"d:\utils\done\";
            if (CDRom.IsValidDriveNumber(game.chosenDrive))
                cd = new CDDrive(game.chosenDrive);

            MusicPlayer.EnableMusicFinishedCallback();
            SdlDotNet.Core.Events.MusicFinished +=new EventHandler<MusicFinishedEventArgs>(currentFinished);
        }

        ~GrvMusic()
        {
            stop();
            if (cd != null)
                cd.Close();
        }

        /// <summary>
        /// Stop any playing music
        /// </summary>
        public void stop()
        {
            if (MusicPlayer.IsPlaying)
            {
                MusicPlayer.Stop();
            }

            if (music != null)
                music.Close();

            if (loop != null)
                loop.Close();

            if (cd != null && cd.Status == CDStatus.Playing)
                cd.Stop(); 
        }

        private void currentFinished(object sender, MusicFinishedEventArgs e)
        {
            if (loop != null)
            {
                if (music != null)
                    music.Close();

                music = loop;
                loop = null;
                MusicPlayer.Load(music);
                MusicPlayer.Play(1);
            }

        }

        /// <summary>
        /// Sets the volume for the midi playback
        /// </summary>
        /// <param name="vol"></param>
        /// <param name="arg2"></param>
        public void setVolume(ushort vol, ushort arg2)
        {
            MusicPlayer.Volume = vol;

        }
        /// <summary>
        /// Start a MIDI playing
        /// </summary>
        /// <param name="filename">XMI filename to play</param>
        public void MIDI(string filename)
        {
            filename = filename.Substring(0, filename.IndexOf('.')) + ".mid";
            if (!File.Exists(path + filename))
            {
                Console.WriteLine(filename + " doesn't exist");
                if (!File.Exists(path + filename))
                {
                    return;
                }
            }

            stop();
            music = new Music(path + filename);
            MusicPlayer.Load(music);
            MusicPlayer.Play(true);
        }

        /// <summary>
        /// Set the loop music
        /// </summary>
        /// <param name="filename">XMI filename to loop</param>
        public void MIDILoop(string filename)
        {
            filename = filename.Substring(0, filename.IndexOf('.')) + ".mid";
            if (!File.Exists(path + filename))
            {
                Console.WriteLine(filename + " doesn't exist");
                if (!File.Exists(path + filename))
                {
                    return;
                }
            }
            try
            {
                if (loop != null)
                    loop.Close();

                loop = new Music(path + filename);
            }
            catch
            {
            }
        }

        /// <summary>
        /// Play audio CD
        /// </summary>
        public void CD()
        {
            stop();
            if (cd != null)
                cd.Close();

            if (CDRom.IsValidDriveNumber(game.chosenDrive))
                cd = new CDDrive(game.chosenDrive);

            if (cd == null)
                return;

            if (cd.Status == CDStatus.Stopped)
            {
                int i = 0;
                for (; i < cd.NumberOfTracks && !cd.IsAudioTrack(i); i++) ;

                if (i == cd.NumberOfTracks)
                {
                    // No audio
                    return;
                }
                else
                {
                    cd.Play(i, 1);
                }
            }




        }
    }
}
